package com.basic.business.smb.service.impl;

import com.basic.business.common.constants.Constants;
import com.basic.business.common.redis.RedisCacheUtils;
import com.basic.business.common.utils.CifsContextPool;
import com.basic.business.smb.domain.TSmbMgmt;
import com.basic.business.smb.mapper.TSmbMgmtMapper;
import com.basic.business.smb.service.ITSmbMgmtService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * 业务-SMB管理Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TSmbMgmtServiceImpl implements ITSmbMgmtService {

    @Resource
    private TSmbMgmtMapper tSmbMgmtMapper;

    /**
     * 项目启动时，初始化SMB信息缓存
     */
    @PostConstruct
    public void init(){
        resetSmbCache();
    }

    public TSmbMgmt getNormalMountSmbInfo() {
        return tSmbMgmtMapper.getNormalMountSmbInfo();
    }

    public void resetSmbCache() {
        // 清空缓存
        RedisCacheUtils.clearCache(Constants.SMB_REDIS_KEY + "*");
        // 加载缓存
        loadingSmbCache();
    }

    /**
     * 加载SMB信息缓存
     */
    private void loadingSmbCache() {
        TSmbMgmt tSmbMgmt = getNormalMountSmbInfo();
        RedisCacheUtils.setCacheObject(Constants.SMB_REDIS_KEY, tSmbMgmt);

        // 初始化连接池
        CifsContextPool.initPool(tSmbMgmt.getUserName(), tSmbMgmt.getPassword());
    }
}
